package com.jingdianjichi.circle.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.entity.Result;
import com.jingdianjichi.circle.api.enums.IsDeletedFlagEnum;
import com.jingdianjichi.circle.api.req.GetShareCommentReq;
import com.jingdianjichi.circle.api.req.RemoveShareCommentReq;
import com.jingdianjichi.circle.api.req.SaveShareCommentReplyReq;
import com.jingdianjichi.circle.api.vo.ShareCommentReplyVO;
import com.jingdianjichi.circle.server.entity.po.ShareCommentReply;
import com.jingdianjichi.circle.server.entity.po.ShareMoment;
import com.jingdianjichi.circle.server.sensitive.WordFilter;
import com.jingdianjichi.circle.server.service.ShareCommentReplyService;
import com.jingdianjichi.circle.server.service.ShareMessageService;
import com.jingdianjichi.circle.server.service.ShareMomentService;
import com.jingdianjichi.practice.server.util.LoginUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 评论及回复信息(ShareCommentReply)表控制层
 *
 * @author makejava
 * @since 2024-06-07 15:50:51
 */
@RestController
@RequestMapping("/share/comment")
@Slf4j
public class ShareCommentController {
    @Resource
    private ShareCommentReplyService shareCommentReplyService;

    @Resource
    private ShareMomentService shareMomentService;

    @Resource
    private WordFilter wordFilter;

    @Resource
    private ShareMessageService shareMessageService;
    /**
     * 获取该动态下评论列表
     */
    @GetMapping(value = "/list")
    public Result<List<ShareCommentReplyVO>> list(@RequestBody GetShareCommentReq req){
        try {
            if(log.isInfoEnabled()){
                log.info("获取评论列表入参:{}", req);
            }
            Preconditions.checkArgument(Objects.nonNull(req),"入参不能为空");
            Preconditions.checkArgument(Objects.nonNull(req.getId()), "评论id不能为空");
            List<ShareCommentReplyVO> result = shareCommentReplyService.listComment(req);
            log.info("获取评论列表出参:{}", result);
            return Result.ok(result);
        }
        catch (IllegalArgumentException e){
            log.error("获取评论列表入参校验失败:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
        catch (Exception e) {
            log.error("获取评论列表失败:{}", e.getMessage(), e);
            return Result.fail("获取参数列表失败");
        }

    }

    /**
     * 添加评论回复
     * @param req
     * @return
     */
    @PostMapping(value = "/save")
    public Result<Boolean> save(@RequestBody SaveShareCommentReplyReq req) {
        try {
            if (log.isInfoEnabled()){
                log.info("发布内容入参:{}", req);
            }
            Preconditions.checkArgument(Objects.nonNull(req), "入参不能为空");
            Preconditions.checkArgument(Objects.nonNull(req.getMomentId()), "评论id不能为空");
            Preconditions.checkArgument(Objects.nonNull(req.getReplyType()), "回复类型不能为空");
            Preconditions.checkArgument(Objects.nonNull(req.getTargetId()), "回复对象id不能为空");
            ShareMoment moment = shareMomentService.getById(req.getMomentId());
            Preconditions.checkArgument(Objects.nonNull(moment) && moment.getIsDeleted() != IsDeletedFlagEnum.DELETE.getCode(), "评论不存在");
            Preconditions.checkArgument(Objects.nonNull(req.getContent()) || Objects.nonNull(req.getPicUrlList()), "评论内容不能为空");
            wordFilter.check(req.getContent());
            Boolean result = shareCommentReplyService.saveComment(req);
            if (log.isInfoEnabled()){
                log.info("发布内容结果:{}", result);
            }
//            如果类型是评论，则走评论的业务逻辑
            if (req.getReplyType() == 1){
                shareMessageService.comment(LoginUtil.getLoginId(), moment.getCreatedBy(), moment.getId());
            }
            else{
                LambdaQueryWrapper<ShareCommentReply> query = Wrappers.<ShareCommentReply>lambdaQuery().eq(ShareCommentReply::getId, req.getTargetId())
                        .select(ShareCommentReply::getCreatedBy);
                ShareCommentReply reply = shareCommentReplyService.getOne(query);
                shareMessageService.reply(LoginUtil.getLoginId(), reply.getCreatedBy(), moment.getId());

            }
            return Result.ok(result);
        } catch (IllegalArgumentException e){
            log.error("参数异常:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
        catch (Exception e) {
            log.error("发布内容异常:{}", e.getMessage(), e);
            return Result.fail("发布内容异常！");
        }


    }
    /**
     * 删除评论回复
     */
    @PostMapping(value = "/remove")
    public Result<Boolean> remove(@RequestBody RemoveShareCommentReq req) {
        try {
            if(log.isInfoEnabled()){
                log.info("删除评论入参:{}", req);
            }
            Preconditions.checkArgument(Objects.nonNull(req), "入参不能为空");
            Preconditions.checkArgument(Objects.nonNull(req.getId()), "评论id不能为空");
            Preconditions.checkArgument(Objects.nonNull(req.getReplyType()), "回复类型不能为空");
            Boolean result = shareCommentReplyService. removeComment(req);
            if(log.isInfoEnabled()){
                log.info("删除评论结果:{}", result);
            }
            return Result.ok(result);
        }
        catch (IllegalArgumentException e){
            log.error("参数异常:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
        catch (Exception e){
            log.error("删除评论异常:{}", e.getMessage(), e);
            return Result.fail("删除评论异常！");
        }

}
}

